<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="供应商名称" prop="supName">
            <el-input
              v-model="queryParams.supName"
              placeholder="请输入供应商名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="联系人" prop="supContact">
            <el-input
              v-model="queryParams.supContact"
              placeholder="请输入联系人"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="电话" prop="supTel">
            <el-input
              v-model="queryParams.supTel"
              placeholder="请输入电话"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="供应商状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!--★★★★★★★★★★★★★★★[表单上方增删改按钮区]★★★★★★★★★★★★★★★-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['drug:supplier:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['drug:supplier:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['drug:supplier:remove']"
            >删除</el-button>
          </el-col>
        </el-row>

        <!--★★★★★★★★★★★★★★★[表单展示区域]★★★★★★★★★★★★★★★-->
        <el-table v-loading="loading" :data="suppliers" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="供应商ID" align="center" key="supId" prop="supId"  />
          <el-table-column label="供应商名称" align="center" key="supName" prop="supName"  :show-overflow-tooltip="true" />
          <el-table-column label="联系人" align="center" key="supContact" prop="supContact"  :show-overflow-tooltip="true" />
          <el-table-column label="手机" align="center" key="supMobile" prop="supMobile"  :show-overflow-tooltip="true" />
          <el-table-column label="电话" align="center" key="supTel" prop="supTel"  :show-overflow-tooltip="true" />
          <el-table-column label="银行账号" align="center" key="supAccount" prop="supAccount"  :show-overflow-tooltip="true" />
          <el-table-column label="地址" align="center" key="supAddress" prop="supAddress"  :show-overflow-tooltip="true" />
          <el-table-column prop="status" label="状态" :formatter="statusFormat" width="80"></el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['drug:factories:edit']"
              >修改</el-button>
              <el-button
                v-if="scope.row.supId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['drug:factories:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!--★★★★★★★★★★★★★★★[新增修改表单区]★★★★★★★★★★★★★★★-->
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supName">
              <el-input v-model="form.supName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="supContact">
              <el-input v-model="form.supContact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机" prop="supMobile">
              <el-input v-model="form.supMobile" placeholder="请输入手机" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="supTel">
              <el-input v-model="form.supTel" placeholder="请输入手机" maxlength="15" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="supAccount">
              <el-input v-model="form.supAccount" placeholder="请输入关键字" maxlength="15" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="supAddress">
              <el-input v-model="form.supAddress" placeholder="请输入地址" maxlength="15" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
/**★★★★★★★★★★★★★★★[导入函数包区域]★★★★★★★★★★★★★★★*/
import { listSuppliers , addSupplier , getFactories , updateSupplier , delSupplier} from "@/api/drug/supplier";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

/**★★★★★★★★★★★★★★★[发布属性以及函数区域区]★★★★★★★★★★★★★★★*/
export default {
  name: "index",
  data() {
    return {
      loading: true,// 遮罩层
      ids: [],// 选中数组
      single: true,// 非单个禁用
      multiple: true,// 非多个禁用
      showSearch: true,// 显示搜索条件
      total: 0,// 总条数
      suppliers: null,// 用户表格数据
      title: "",// 弹出层标题
      dateRange: [],// 日期范围
      deptOptions: undefined,// 部门树选项
      open: false,// 是否显示弹出层
      deptName: undefined,// 部门名称
      initPassword: undefined,// 默认密码
      facBuildTime: [],// 日期范围
      statusOptions: [],// 状态数据字典
      sexOptions: [],// 性别状态字典
      postOptions: [],// 岗位选项
      roleOptions: [],// 角色选项

      // 表单参数
      form: {
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询表单参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supName: undefined,
        supContact: undefined,
        supMobile: undefined,
        status: undefined,
        supId: undefined
      },
      rules: {// 表单校验
        supName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        supContact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        supMobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ]
      }
    };
  },

/**★★★★★★★★★★★★★★★[VUE封装的方法区]★★★★★★★★★★★★★★★*/
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查询厂家列表 */
    getList() {
      this.loading = true;
      listSuppliers(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.suppliers = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supId: undefined,
        supName: undefined,
        supContact: undefined,
        supMobile: undefined,
        supTel: undefined,
        supAccount: undefined,
        supAddress: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supId = row.supId || this.ids;
      getFactories(supId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supId != undefined) {
            updateSupplier(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supIds = row.supId || this.ids;
      this.$confirm('是否确认删除生产厂编号为"' + supIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(supIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    }
  }
};
</script>
